import Vue from 'vue';
import Router from 'vue-router';
import { Notify } from 'vant'
Vue.use(Router);

const routes = [
  {
    path: '/',
    redirect: '/search'
  },
  {
    name: 'home',
    component: () => import('./view/home'),
    meta: {
      title: '购物车',
      requireAuth:true
    }
  },
  {
    name: 'search',
    component: () => import('./view/search/index copy.vue'),
    meta: {
      title: '点餐'
    }
  },
  {
    name: 'order',
    component: () => import('./view/order'),
    meta: {
      title: '订单',
      requireAuth:true
    }
  },
  {
    name: 'pay',
    component: () => import('./view/pay'),
    meta: {
      title: '订单结算',
      TabbarShow: true,
      requireAuth:true
    }
  },
  {
    name: 'paymethod',
    component: () => import('./view/paymethod'),
    meta: {
      title: '在线支付',
      TabbarShow: true,
      requireAuth:true
    }
  },
  {
    name: 'alipayresult',
    component: () => import('./view/alipayresult'),
    meta: {
      title: '确认支付',
      TabbarShow: true,
      requireAuth:true
    }
  },
  {
    name: 'payment',
    component: () => import('./view/payment'),
    meta: {
      title: '余额支付',
      TabbarShow: true,
      requireAuth:true
    }
  },
  {
    name: 'paymentsuccess',
    component: () => import('./view/paymentsuccess/index.vue'),
    meta: {
      title: '支付成功',
      TabbarShow: true,
      requireAuth:true
    }
  },
  {
    name: 'setpaypassword',
    component: () => import('./view/setpaypassword/index.vue'),
    meta: {
      title: '设置支付密码',
      TabbarShow: true,
      requireAuth:true
    }
  },
  {
    name: 'userinfo',
    component: () => import('./view/userinfo/index.vue'),
    meta: {
      title: '个人信息',
      TabbarShow: true,
      requireAuth:true
    }
  },
  {
    name: 'user',
    component: () => import('./view/user'),
    meta: {
      title: '会员中心',
      requireAuth:true
    }
  },

  {
    name: 'login',
    component: () => import('./view/login'),

    meta: {
      title: '会员登录',
      TabbarShow: true,// 不需要显示 底部导航
    }
  },

  {
    name: 'cart',
    component: () => import('./view/cart'),
    meta: {
      title: '购物车',
      requireAuth:true
    }
  },
  {
    name: 'goods',
    component: () => import('./view/goods'),
    meta: {
      title: '商品详情'
    }
  }
];

// add route path
routes.forEach(route => {
  route.path = route.path || '/' + (route.name || '');
});

const router = new Router({
  routes,
  mode: 'hash'
});

router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title;
  const requireAuth = to.meta && to.meta.requireAuth;
  if (title) {
    document.title = title;
  }
  if (requireAuth) {
    if (!window.sessionStorage.getItem('token')) {
      Notify({
        type: 'primary',
        message: "未登录,请先登录"
      })
      return next('/login')
    }
  }
  next();
});

export {
  router
};
